#include "lendmsg.h"
#include "ui_lendmsg.h"

LendMsg::LendMsg(Lend_t data, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LendMsg)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_StyledBackground, true);
    this->setObjectName("widget");
    this->setStyleSheet("#widget{background-image: url(:/res/beijing.jpg);}");

    QStringList list;
    User_t user = Sqlite::Get()->Select_user(data.id);
    if(!user.option.isEmpty())
    {
        QJsonParseError err;
        QJsonDocument doc = QJsonDocument::fromJson(user.option.toUtf8(), &err);
        if(err.error != QJsonParseError::NoError)
        {
            welog_debug() << "json error";
        }
        else
        {
            QJsonObject obj = doc.object();
            QJsonValue value = obj.value("orderList");
            if(value.isArray())
            {
                QJsonArray array = value.toArray();
                for (int i = 0; i < array.size(); ++i)
                {
                    list.append(array.at(i).toObject().value("option").toString());
                }
            }
        }
    }

    if(list.size())
    {
        ui->label_3->setText("想预约：《" + list.at(0) + "》等图书");
    }

    if(data.book.isEmpty())
    {
        ui->label_2->setText("借阅图书：无");
        ui->label_4->setText("借阅日期：无");
        ui->label_5->setText("归还日期：无");        
    }
    else
    {
        ui->label_2->setText("借阅图书：《" + data.book + "》");
        QDateTime time  = QDateTime::fromTime_t(data.lend.toUInt());
        QString begin = time.toString("yyyy.MM.dd hh:mm:ss.zzz ddd");
        QString str = time.addDays(7).toString("yyyy-MM-dd hh:mm:ss");
        ui->label_4->setText("借阅日期：" + begin);
        ui->label_5->setText("归还日期：" + str);
    }
}

LendMsg::~LendMsg()
{
    delete ui;
}

void LendMsg::on_pushButton_clicked()
{
    this->close();
}
